import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import { DNavBar, DLookImages } from '@/components'
import { AtButton } from 'taro-ui'
import './inspectionphotos.scss'
import http from '@/api'
const itembg = require('../assets/pic@2x.png')
export default class ApplicationinfoBox extends Component {
    constructor(props) {
        super(props)
        this.state = {
            id: '',
            active: '',
            title: '入库照片',
            list: [],
            status: '',
            companycar: {
                itemStatus: false,
                itemUrl: ''
            }
        }
    }
    componentWillMount() {
        let { id = '', active = '', status } = this.$router.params
        this.setState({
            id,
            active,
            status,
            title: this.getTitle(active) || '入库照片'
        })
        this.getFinancedPhoto(id, active)
    }
    getTitle(active) {
        return {
            $1: '入库照片',
            $2: '合同凭证'
        }[`$${active}`]
    }
    getFinancedPhoto(id, active) {
        let obj = {
            afBusstype: active,
            refAmId: id
        }
        let { list } = this.state
        http.getFinancedPhoto(obj).then(res => {
            if (!res) {
                return
            }
            res.map(item => {
                list.push({
                    url: item.afUrl
                })
            })
            this.setState({
                list
            })
        })
    }
    handleConfirm() {
        this.confirmFinancedCar(this.state.id)
    }

    handleFinish() {
        this.getRightInfo(this.state.id)
    }

    //确认验车
    confirmFinancedCar(id) {
        http.confirmFinancedCar({ id: id }).then(res => {
            Taro.redirectTo({
                url: `/pages/itemdetail/financedetail?id=${this.state.id}&status=4`
            })
        })
    }
    //完成交易
    getRightInfo(id) {
        http.getRightInfo({ id }).then(res => {
            Taro.redirectTo({
                url: `/pages/itemdetail/financedetail?id=${this.state.id}&status=6`
            })
        })
    }

    onLookHandle(url, i, arr) {
        if (window.bridge) {
            let _images = []
            arr.map(item => {
                _images.push(item.url)
            })
            window.bridge.callHandler('previewPhoto', { "index": i, "images": _images }, function responseCallback() { })
        } else {
            this.setState({
                companycar: {
                    ...this.state.companycar,
                    itemStatus: true,
                    itemUrl: url
                }
            })
        }
    }

    onLookItem() {
        this.setState({
            companycar: {
                ...this.state.companycar,
                itemStatus: false,
                itemUrl: ''
            }
        })
    }

    render() {
        const {
            title,
            list,
            active,
            status,
            companycar
        } = this.state
        return (
            <View className='b_inspectionphotos'>
                <DNavBar title={title}></DNavBar>
                <View className='b_inspectionphotos__box'>
                </View>
                <View className='b_inspectionphotos__images'>

                    <View className="b_inspectionphotos__images_enterpris">
                        <View className='b_inspectionphotos__images_enterpris_item'>
                            {
                                list && list.map((item, i) => {
                                    return <Image key={i} src={item.url} onClick={this.onLookHandle.bind(this, item.url, i, list)} className='item_image'></Image>
                                })
                            }
                        </View>
                    </View>
                </View>
                <View className='b_inspectionphotos__footer'>
                    {status === '1' &&
                        <AtButton className='item_btn' onClick={active === '1' ? this.handleConfirm.bind(this) : this.handleFinish.bind(this)}>{active === '1' ? '完成验车' : '交易完成'}</AtButton>
                    }
                </View>
                <DLookImages status={companycar.itemStatus} url={companycar.itemUrl} onLookHandle={this.onLookItem.bind(this)}></DLookImages>
            </View>
        )
    }
}
